package showgo.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import showgo.modell.Manager;
import showgo.modell.stamm.Person;

public class PersonDialog extends JDialog implements ActionListener,
	KeyListener, MouseListener, ItemListener {

    /**
     * 
     */
    private static final long serialVersionUID = -7532725158060791573L;
    private JFileChooser fileChooser;

    private int index;

    private javax.swing.JButton abbrechenButton;
    private javax.swing.JButton beendenButton;
    private java.awt.Label gebLabel;
    @SuppressWarnings("rawtypes")
    private javax.swing.JComboBox geschlechtComboBox;
    private java.awt.Label geschlechtLabel;
    private javax.swing.JScrollPane jScrollPane1;
    @SuppressWarnings("rawtypes")
    private javax.swing.JComboBox jahrComboBox;
    private java.awt.Label jahrLabel;
    private javax.swing.JButton ladenPersonButton;
    @SuppressWarnings("rawtypes")
    private javax.swing.JComboBox monatComboBox;
    private java.awt.Label monatLabel;
    private java.awt.Label nameLabel;
    private java.awt.TextField nameTextField;
    private javax.swing.JButton neuePersonButton;
    private PersonTable personJTable;
    @SuppressWarnings("rawtypes")
    private javax.swing.JComboBox tagComboBox;
    private java.awt.Label tagLabel;
    private java.awt.Label wortzahlLabel;
    private IntegerTextField wortzahlTextField;
    private javax.swing.JButton entfernePersonButton;

    public PersonDialog() {
	super();
	setTitle("Personen bearbeiten:");
	initComponents();

	initTable();

	fileChooser = new JFileChooser("resource");
	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	fileChooser.setDialogTitle("Personen-Datei wählen");
	FileFilter txt = new FileFilter() {

	    @Override
	    public String getDescription() {
		return "TXT- Dateien(*.txt)";
	    }

	    @Override
	    public boolean accept(File f) {
		return f.getName().toLowerCase().endsWith("txt")
			|| f.isDirectory();
	    }
	};
	fileChooser.setFileFilter(txt);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void initComponents() {

	setPreferredSize(new java.awt.Dimension(900, 450));
	// DialogFrame kann nicht maximiert werden
	setResizable(false);

	personJTable = new PersonTable();
	personJTable.addMouseListener(this);
	personJTable.addKeyListener(this);
	personJTable.setModel(new PersonTableModel());

	jScrollPane1 = new javax.swing.JScrollPane();
	jScrollPane1.setViewportView(personJTable);

	nameTextField = new java.awt.TextField();
	geschlechtLabel = new java.awt.Label();
	tagLabel = new java.awt.Label();
	monatLabel = new java.awt.Label();
	jahrLabel = new java.awt.Label();
	gebLabel = new java.awt.Label();
	nameLabel = new java.awt.Label();
	wortzahlLabel = new java.awt.Label();

	wortzahlTextField = new IntegerTextField(false);

	jahrComboBox = new javax.swing.JComboBox();
	jahrComboBox.setEditable(false);
	initJahre();
	jahrComboBox.setSelectedItem("1975");
	jahrComboBox.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		JComboBox jahrComboBox = (JComboBox) e.getSource();
		Object selectedItemJahrComboBox = jahrComboBox
			.getSelectedItem();
		initTage(selectedItemJahrComboBox);
	    }
	});

	monatComboBox = new javax.swing.JComboBox();
	monatComboBox.setEditable(false);
	monatComboBox.setModel(new javax.swing.DefaultComboBoxModel(
		new String[] { "01", "02", "03", "04", "05", "06", "07", "08",
			"09", "10", "11", "12" }));
	monatComboBox.addItemListener(this);
	monatComboBox.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		JComboBox monatComboBox = (JComboBox) e.getSource();
		Object selectedItemMonatComboBox = monatComboBox
			.getSelectedItem();
		initTage(selectedItemMonatComboBox);
	    }
	});

	tagComboBox = new javax.swing.JComboBox();
	tagComboBox.setEditable(false);
	tagComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
		"01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
		"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
		"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
		"31" }));

	geschlechtComboBox = new javax.swing.JComboBox();
	geschlechtComboBox.setEditable(false);
	geschlechtComboBox.setModel(new javax.swing.DefaultComboBoxModel(
		new String[] { "maennlich", "weiblich" }));
	geschlechtComboBox.addItemListener(this);

	nameLabel = new java.awt.Label();

	wortzahlLabel = new java.awt.Label();
	wortzahlTextField = new IntegerTextField();

	geschlechtLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	geschlechtLabel.setText("Geschlecht");

	tagLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	tagLabel.setText("Tag");

	monatLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	monatLabel.setText("Monat");

	jahrLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	jahrLabel.setText("Jahr");

	gebLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	gebLabel.setText("Geburtsdatum");

	nameLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	nameLabel.setText("Name");

	wortzahlLabel.setFont(new java.awt.Font("Dialog", 1, 14));
	wortzahlLabel.setText("Wortzahl");

	neuePersonButton = new javax.swing.JButton();
	neuePersonButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	neuePersonButton.setText("Person anlegen");
	neuePersonButton.addActionListener(this);

	beendenButton = new javax.swing.JButton();
	beendenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	beendenButton.setText("Beenden");
	beendenButton.addActionListener(this);

	abbrechenButton = new javax.swing.JButton();
	abbrechenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	abbrechenButton.setText("Abbrechen");
	abbrechenButton.addActionListener(this);

	ladenPersonButton = new javax.swing.JButton();
	ladenPersonButton.addActionListener(this);
	ladenPersonButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	ladenPersonButton.setText("Personen laden");
	ladenPersonButton.setActionCommand("Person auswaehlen");

	entfernePersonButton = new javax.swing.JButton();
	entfernePersonButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	entfernePersonButton.setText("Person entfernen");
	entfernePersonButton.addActionListener(this);

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(
									jScrollPane1))
						.addGroup(
							layout.createSequentialGroup()
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
											javax.swing.GroupLayout.Alignment.TRAILING,
											layout.createSequentialGroup()
												.addContainerGap()
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															nameLabel,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
															nameTextField,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															155,
															javax.swing.GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															jahrLabel,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
															jahrComboBox,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGap(17,
													17,
													17)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															monatLabel,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
															monatComboBox,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															tagComboBox,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
															tagLabel,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGap(18,
													18,
													18)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															geschlechtLabel,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
															geschlechtComboBox,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
															layout.createSequentialGroup()
																.addComponent(
																	wortzahlLabel,
																	javax.swing.GroupLayout.DEFAULT_SIZE,
																	javax.swing.GroupLayout.DEFAULT_SIZE,
																	Short.MAX_VALUE)
																.addGap(26,
																	26,
																	26))
														.addComponent(
															wortzahlTextField,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															61,
															javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addGroup(
											layout.createSequentialGroup()
												.addGap(175,
													175,
													175)
												.addComponent(
													gebLabel,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(
											javax.swing.GroupLayout.Alignment.TRAILING,
											layout.createSequentialGroup()
												.addContainerGap()
												.addComponent(
													neuePersonButton)
												.addGap(30,
													30,
													30)))
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
											layout.createSequentialGroup()
												.addGap(10,
													10,
													10)
												.addComponent(
													abbrechenButton,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													127,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
												.addComponent(
													beendenButton,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													154,
													javax.swing.GroupLayout.PREFERRED_SIZE))
										.addComponent(
											entfernePersonButton,
											javax.swing.GroupLayout.Alignment.TRAILING,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											154,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											ladenPersonButton,
											javax.swing.GroupLayout.Alignment.TRAILING,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											154,
											javax.swing.GroupLayout.PREFERRED_SIZE))))
				.addContainerGap()));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jScrollPane1,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					192, Short.MAX_VALUE)
				.addGap(18, 18, 18)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.TRAILING)
						.addGroup(
							layout.createSequentialGroup()
								.addComponent(
									gebLabel,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									nameLabel,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									nameTextField,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE))
						.addGroup(
							layout.createSequentialGroup()
								.addComponent(
									ladenPersonButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									38,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.TRAILING)
										.addComponent(
											wortzahlLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											jahrLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											monatLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											tagLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											geschlechtLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											entfernePersonButton,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											38,
											javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
											javax.swing.GroupLayout.Alignment.TRAILING,
											layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
													jahrComboBox,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
													monatComboBox,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
													tagComboBox,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
													geschlechtComboBox,
													javax.swing.GroupLayout.PREFERRED_SIZE,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													javax.swing.GroupLayout.PREFERRED_SIZE))
										.addComponent(
											wortzahlTextField,
											javax.swing.GroupLayout.Alignment.TRAILING,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.PREFERRED_SIZE))))
				.addGap(18, 18, 18)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(
							beendenButton,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							38,
							javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(
							abbrechenButton,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							38,
							javax.swing.GroupLayout.PREFERRED_SIZE)
						.addComponent(
							neuePersonButton,
							javax.swing.GroupLayout.PREFERRED_SIZE,
							38,
							javax.swing.GroupLayout.PREFERRED_SIZE))
				.addContainerGap()));

	pack();
    }

    @SuppressWarnings("unchecked")
    private void initJahre() {
	int jahr = 1910;
	while (jahr != 2013) {
	    jahrComboBox.addItem(String.valueOf(jahr));
	    jahr++;
	}
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void initTage(Object selectedItem) {
	// manche Monate haben 31 Tage
	if (Integer.valueOf((String) monatComboBox.getSelectedItem()) == 1
		|| monatComboBox.getSelectedItem().equals("03")
		|| monatComboBox.getSelectedItem().equals("05")
		|| monatComboBox.getSelectedItem().equals("07")
		|| monatComboBox.getSelectedItem().equals("08")
		|| monatComboBox.getSelectedItem().equals("10")
		|| monatComboBox.getSelectedItem().equals("12")) {
	    tagComboBox.setModel(new javax.swing.DefaultComboBoxModel(
		    new String[] { "01", "02", "03", "04", "05", "06", "07",
			    "08", "09", "10", "11", "12", "13", "14", "15",
			    "16", "17", "18", "19", "20", "21", "22", "23",
			    "24", "25", "26", "27", "28", "29", "30", "31" }));
	}// Schaltjahr beachten
	else if (monatComboBox.getSelectedItem().equals("02")
		&& Integer.valueOf((String) jahrComboBox.getSelectedItem()
			.toString()) % 4 == 0) {
	    tagComboBox.setModel(new javax.swing.DefaultComboBoxModel(
		    new String[] { "01", "02", "03", "04", "05", "06", "07",
			    "08", "09", "10", "11", "12", "13", "14", "15",
			    "16", "17", "18", "19", "20", "21", "22", "23",
			    "24", "25", "26", "27", "28", "29" }));
	} else if (monatComboBox.getSelectedItem().equals("02")) {
	    tagComboBox.setModel(new javax.swing.DefaultComboBoxModel(
		    new String[] { "01", "02", "03", "04", "05", "06", "07",
			    "08", "09", "10", "11", "12", "13", "14", "15",
			    "16", "17", "18", "19", "20", "21", "22", "23",
			    "24", "25", "26", "27", "28" }));
	} else {
	    tagComboBox.setModel(new javax.swing.DefaultComboBoxModel(
		    new String[] { "01", "02", "03", "04", "05", "06", "07",
			    "08", "09", "10", "11", "12", "13", "14", "15",
			    "16", "17", "18", "19", "20", "21", "22", "23",
			    "24", "25", "26", "27", "28", "29", "30" }));
	}

    }

    private boolean gueltigeEingabe() {
	if (nameTextField.getText().isEmpty()) {
	    JOptionPane.showMessageDialog(null, "Bitte Namen eingeben!");
	    return false;
	} else if (wortzahlTextField.getText().isEmpty()) {
	    JOptionPane.showMessageDialog(null, "Bitte Wortzahl eingeben!");
	    return false;
	}
	return true;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
	// bei Doppelklick auf eine Zeile in der Tabelle die Daten des
	// zugehörigen Elements in den Eingabefeldern anzeigen
	if (e.getClickCount() == 2) {
	    index = personJTable.rowAtPoint(e.getPoint());
	    if (index > -1) {
		ladeElement(index);
	    }
	    neuePersonButton.setText("Person ändern");
	}
    }

    private void ladeElement(int index) {
	leereFelder();
	nameTextField.setText(Manager.getInstance().getPersonen().get(index)
		.getName());
	wortzahlTextField.setValue(Manager.getInstance().getPersonen()
		.get(index).getWortzahlMerken());

	String gebDatum = Manager.getInstance().getPersonen().get(index)
		.getGebDatum();
	String tag = gebDatum.substring(0, 2);
	String monat = gebDatum.substring(3, 5);
	String jahr = gebDatum.substring(6, gebDatum.length());

	jahrComboBox.setSelectedItem(jahr);
	monatComboBox.setSelectedItem(monat);
	tagComboBox.setSelectedItem(tag);
    }

    private void leereFelder() {
	nameTextField.setText("");
	wortzahlTextField.setText("");
	nameTextField.requestFocusInWindow();
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
	if (e.getSource() == personJTable) {
	    if (e.getKeyCode() == KeyEvent.VK_DELETE
		    && personJTable.getSelectedRow() > -1) {
		Person person = Manager.getInstance().getPersonen()
			.get(personJTable.getSelectedRow());
		entfernePerson(person);
	    }
	}
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == abbrechenButton) {
	    int ret = JOptionPane.showConfirmDialog(null,
		    "Wollen Sie die Änderungen verwerfen?",
		    "Sitzung Abbrechen!", JOptionPane.YES_NO_OPTION);
	    if (ret == JOptionPane.YES_OPTION) {
		dispose();
	    }
	} else if (e.getSource() == ladenPersonButton) {
	    JOptionPane
		    .showMessageDialog(
			    null,
			    "Die ausgewählte Datei soll zeilenweise wie folgt aufgebaut sein:\n\nMax Mustermann männlich 01.01.1990 300 Wörter\n\n- jeweils nur ein Leerzeichen zur Trennung\n- die Begriffe männlich oder weiblich als Geschlecht verwenden\n- Datumsangaben der Form dd.mm.yyyy beachten");

	    int ret = fileChooser.showOpenDialog(this);
	    if (ret == JFileChooser.APPROVE_OPTION) {
		File file = fileChooser.getSelectedFile();
		ladePersonen(file.getPath());
		initTable();
	    }
	} else if (e.getSource() == neuePersonButton) {
	    if (gueltigeEingabe()) {
		String name = nameTextField.getText();
		String geschlecht = geschlechtComboBox.getSelectedItem()
			.toString();
		int zahl = Integer.valueOf(wortzahlTextField.getText());
		String gebDatum = tagComboBox.getSelectedItem().toString()
			+ "." + monatComboBox.getSelectedItem().toString()
			+ "." + jahrComboBox.getSelectedItem().toString();
		if (neuePersonButton.getText().equals("Person ändern")) {
		    // dann wurde eine Person aus der Liste gewaehlt
		    Manager.getInstance().getPersonen().get(index)
			    .setName(name);
		    Manager.getInstance().getPersonen().get(index)
			    .setGeschlecht(geschlecht);
		    Manager.getInstance().getPersonen().get(index)
			    .setGebDatum(gebDatum);
		    Manager.getInstance().getPersonen().get(index)
			    .setWortzahlMerken(zahl);
		} else {
		    Person person = new Person(name, gebDatum, geschlecht, zahl);
		    fuegePersonHinzu(person);
		}
		initTable();
		leereFelder();
		neuePersonButton.setText("Person anlegen");
	    }
	} else if (e.getSource() == beendenButton) {
	    Manager.getInstance().fireListChanged();
	    this.dispose();
	} else if (e.getSource() == entfernePersonButton) {
	    if (personJTable.getSelectedRow() > -1) {
		Person person = Manager.getInstance().getPersonen()
			.get(personJTable.getSelectedRow());
		entfernePerson(person);
	    }
	}
    }

    private void initTable() {
	if (!Manager.getInstance().getPersonen().isEmpty()) {
	    personJTable.setModel(new PersonTableModel());
	    for (int i = 0; i < Manager.getInstance().getPersonen().size(); i++) {
		this.personJTable.addPerson(Manager.getInstance().getPersonen()
			.get(i));
	    }
	}
    }

    @Override
    public void itemStateChanged(ItemEvent arg0) {
    }

    private void ladePersonen(String path) {
	if (path != null) {
	    try {
		FileReader fr = new FileReader(path);
		BufferedReader br = new BufferedReader(fr);
		String zeile;
		while ((zeile = br.readLine()) != null) {
		    Person person = Manager.getInstance().parsePerson(zeile);
		    fuegePersonHinzu(person);
		}
		br.close();
	    } catch (FileNotFoundException e) {
		e.printStackTrace();
		JOptionPane.showMessageDialog(null,
			"Die ausgewählte Datei wurde nicht gefunden!");
	    } catch (IOException e) {
		e.printStackTrace();
		JOptionPane
			.showMessageDialog(
				null,
				"Die ausgewählte Datei entspricht nicht den geforderten Aufbau!\nDie Datei wird nicht geladen.");

	    }
	}
    }

    public void fuegePersonHinzu(Person person) {
	if (personIistSchonVorhanden(person)) {
	    for (int i = 0; i < Manager.getInstance().getPersonen().size(); i++) {
		if (Manager.getInstance().getPersonen().get(i).getName()
			.equals(person.getName())) {
		    Manager.getInstance().getPersonen().get(i)
			    .setWortzahlMerken(person.getWortzahlMerken());
		}
	    }
	} else {
	    Manager.getInstance().getPersonen().add(person);
	    initTable();
	}
    }

    private boolean personIistSchonVorhanden(Person person) {
	// Gleichheit bei identischen Namen, GebDatum und Geschlecht
	// neue Anzahl der Woerter wird uebernommen
	if (person != null && !Manager.getInstance().getPersonen().isEmpty()) {
	    for (int i = 0; i < Manager.getInstance().getPersonen().size(); i++) {
		if (Manager.getInstance().getPersonen().get(i).getName()
			.equals(person.getName())
			&& Manager.getInstance().getPersonen().get(i)
				.getGebDatum().equals(person.getGebDatum())
			&& Manager.getInstance().getPersonen().get(i)
				.getGeschlecht().equals(person.getGeschlecht())) {
		    return true;
		}
	    }
	}
	return false;
    }

    public void entfernePerson(Person person) {
	if (person != null) {
	    boolean wirdVerwendet = false;
	    // die gewaehlte Person darf nicht in einem Ensemble vorhanden sein,
	    // dass bei einer Inszenierung verwendet wird
	    for (int i = 0; i < Manager.getInstance().getEnsembles().size(); i++) {
		for (int j = 0; j < Manager.getInstance().getEnsembles().get(i)
			.getGruppe().size(); j++) {
		    if (Manager.getInstance().getEnsembles().get(i).getGruppe()
			    .get(j).getName().equals(person.getName())) {
			// dann testen, ob genau dieses Ensemble verwendet wird
			if (Manager.getInstance().ensembleWirdVerwendet(
				Manager.getInstance().getEnsembles().get(i))) {
			    wirdVerwendet = true;
			    JOptionPane
				    .showMessageDialog(
					    null,
					    "Dieses Person wird bei einer Inszenierung verwendet\nund kann deswegen nicht entfernt werden!");
			}
		    }
		}
	    }
	    if (!wirdVerwendet) {
		// Person ist in keiner Inszenierung dabei und kann deswegen
		// entfernt werden.
		int ret = JOptionPane.showConfirmDialog(null,
			"Wollen Sie diese Person wirklich entfernen?",
			"Person entfernen!", JOptionPane.YES_NO_OPTION);
		if (ret == JOptionPane.YES_OPTION) {
		    Manager.getInstance().getPersonen()
			    .remove(personJTable.getSelectedRow());
		    initTable();
		}
	    }
	}
    }
}
